package com.laiketui.admin.store.service.dubbo.supplier;

import com.laiketui.admin.store.api.supplier.SupplierBrandService;
import com.laiketui.common.api.PubliceService;
import com.laiketui.common.mapper.*;
import com.laiketui.common.utils.tool.DateUtil;
import com.laiketui.common.utils.tool.cache.RedisDataTool;
import com.laiketui.common.utils.tool.str.StringUtils;
import com.laiketui.core.cache.RedisUtil;
import com.laiketui.core.common.SplitUtils;
import com.laiketui.core.exception.LaiKeAPIException;
import com.laiketui.core.lktconst.GloabConst;
import com.laiketui.core.utils.tool.DataUtils;
import com.laiketui.domain.lktconst.DictionaryConst;
import com.laiketui.domain.lktconst.ErrorCode;
import com.laiketui.domain.log.AdminRecordModel;
import com.laiketui.domain.mch.AdminModel;
import com.laiketui.domain.message.MessageLoggingModal;
import com.laiketui.domain.product.BrandClassModel;
import com.laiketui.domain.product.ProductClassModel;
import com.laiketui.domain.supplier.SupplierBrandModel;
import com.laiketui.domain.vo.MainVo;
import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 供应商商品分类管理
 *
 * @Author: sunH_
 * @Date: Create in 16:27 2022/9/15
 */
@Service
public class SupplierBrandServiceImpl implements SupplierBrandService {

    private final Logger logger = LoggerFactory.getLogger(SupplierBrandServiceImpl.class);

    @Autowired
    private SupplierBrandModelMapper supplierBrandModelMapper;

    @Autowired
    private BrandClassModelMapper brandClassModelMapper;

    @Autowired
    private SupplierProClassModelMapper supplierProClassModelMapper;

    @Autowired
    private ProductClassModelMapper productClassModelMapper;

    @Autowired
    private PubliceService publiceService;

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public Map<String, Object> auditList(MainVo vo, Integer id, String condition, Integer status, String startTime, String endTime) throws LaiKeAPIException {
        Map<String, Object> resultMap = new HashMap<>(16);
        try {
            Map<String, Object> paramMap = new HashMap<>(16);
            paramMap.put("storeId", vo.getStoreId());
            paramMap.put("add_date_sort", DataUtils.Sort.DESC.toString());
            paramMap.put("page", vo.getPageNo());
            paramMap.put("pageSize", vo.getPageSize());
            if (!Objects.isNull(id)){
                paramMap.put("id", id);
            }
            if (StringUtils.isNotEmpty(condition)){
                paramMap.put("condition", condition);
            }
            if (!Objects.isNull(status)){
                paramMap.put("status", status);
            }
            if (StringUtils.isNotEmpty(startTime)){
                paramMap.put("startTime", startTime);
            }
            if (StringUtils.isNotEmpty(endTime)){
                paramMap.put("endTime", endTime);
            }
            int i = supplierBrandModelMapper.countCondition(paramMap);
            List<Map<String, Object>> list = new ArrayList<>();
            if (i > 0){
                list = supplierBrandModelMapper.selectCondition(paramMap);
                for (Map<String, Object> map : list){
                    map.put("brand_time", DateUtil.dateFormate(MapUtils.getString(map, "brand_time"), GloabConst.TimePattern.YMDHMS));
                    String examine = MapUtils.getString(map, "examine");
                    String examineDesc = "";
                    if (examine.equals(DictionaryConst.ExameStatus.EXAME_WAIT_STATUS)){
                        examineDesc = "待审核";
                    }else if (examine.equals(DictionaryConst.ExameStatus.EXAME_PASS_STATUS)){
                        examineDesc = "审核通过";
                    }else {
                        examineDesc = "审核不通过";
                    }
                    map.put("examineDesc", examineDesc);
                    map.put("brand_pic", publiceService.getImgPath(MapUtils.getString(map, "brand_pic"), vo.getStoreId()));
                    map.put("brand_image", publiceService.getImgPath(MapUtils.getString(map, "brand_image"), vo.getStoreId()));
                    //获取分类
                    String[] cidList = StringUtils.trim(MapUtils.getString(map, "categories"), ",").split(",");
                    List<String> classNames = new ArrayList<>();
                    for (String cid : cidList) {
                        ProductClassModel productClassModel = new ProductClassModel();
                        productClassModel.setCid(Integer.parseInt(cid));
                        productClassModel = productClassModelMapper.selectOne(productClassModel);
                        if (productClassModel != null) {
                            classNames.add(productClassModel.getPname());
                        }
                    }
                    map.put("categories", classNames);
                    map.put("categoriesName", StringUtils.stringImplode(classNames, SplitUtils.DH));
                }
            }
            resultMap.put("total", i);
            resultMap.put("list", list);
        } catch (LaiKeAPIException l) {
            throw l;
        } catch (Exception e) {
            logger.error("审核供应商商品 异常", e);
            throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_WLYC, "网络异常", "examine");
        }

        return resultMap;
    }

    @Autowired
    private MessageLoggingModalMapper messageLoggingModalMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void examine(MainVo vo, Integer id, Integer status, String remark) throws LaiKeAPIException {
        try {
            AdminModel adminModel = RedisDataTool.getRedisAdminUserCache(vo.getAccessId(), redisUtil);
            String text = "";
            String event = "";
            SupplierBrandModel supplierBrandModel = supplierBrandModelMapper.selectByPrimaryKey(id);
            if (Objects.isNull(supplierBrandModel)){
                throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_MBBCZ, "供应商商品品牌信息不存在", "examine");
            }
            supplierBrandModel.setExamine(status);
            if (status.toString().equals(DictionaryConst.ExameStatus.EXAME_NOT_PASS_STATUS)){
                if (StringUtils.isEmpty(remark)){
                    throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_CSCW, "备注不能为空", "examine");
                }
                supplierBrandModel.setRemark(remark);
            }
            supplierBrandModelMapper.updateByPrimaryKeySelective(supplierBrandModel);
            if (status.toString().equals(DictionaryConst.ExameStatus.EXAME_PASS_STATUS)){
                BrandClassModel brandClassModel = new BrandClassModel();
                brandClassModel.setStore_id(supplierBrandModel.getStore_id());
                brandClassModel.setBrand_pic(supplierBrandModel.getBrand_pic());
                brandClassModel.setBrand_image(supplierBrandModel.getBrand_image());
                brandClassModel.setBrand_name(supplierBrandModel.getBrand_name());
                brandClassModel.setBrand_y_name(supplierBrandModel.getBrand_y_name());
                brandClassModel.setProducer(supplierBrandModel.getProducer());
                brandClassModel.setRemarks(supplierBrandModel.getRemarks());
                brandClassModel.setStatus(supplierBrandModel.getStatus());
                brandClassModel.setBrand_time(new Date());
                brandClassModel.setSort(supplierBrandModel.getSort());
                brandClassModel.setRecycle(DictionaryConst.ProductRecycle.NOT_STATUS);
                brandClassModel.setCategories(supplierBrandModel.getCategories());
                //判断品牌名称是否存在
                BrandClassModel brandClassOld = new BrandClassModel();
                brandClassOld.setStore_id(vo.getStoreId());
                brandClassOld.setRecycle(DictionaryConst.ProductRecycle.NOT_STATUS);
                brandClassOld.setBrand_name(supplierBrandModel.getBrand_name());
                int count = brandClassModelMapper.selectCount(brandClassModel);
                if (count > 0) {
                    throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_PPMCYCZ, "品牌名称已存在", "examine");
                }
                //判断所属分类是否存在
                String[] cidList = StringUtils.trim(supplierBrandModel.getCategories(), ",").split(",");
                for (String cid : cidList) {
                    ProductClassModel productClassModel = new ProductClassModel();
                    productClassModel.setCid(Integer.parseInt(cid));
                    productClassModel = productClassModelMapper.selectOne(productClassModel);
                    if (productClassModel == null) {
                        throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_PPSSFLBCZ, "品牌所属分类不存在", "examine");
                    }
                }
                brandClassModelMapper.insertSelective(brandClassModel);
                text = "商品品牌" + supplierBrandModel.getBrand_name() + "审核通过";
                event = "通过了品牌ID："+supplierBrandModel.getBrand_id()+"，并且名称为："+supplierBrandModel.getBrand_name()+"的审核";
            }else {
                text = "商品品牌" + supplierBrandModel.getBrand_name() + "审核拒绝";
                event = "拒绝了品牌ID："+supplierBrandModel.getBrand_id()+"，并且名称为："+supplierBrandModel.getBrand_name()+"的审核";
            }
            //操作日志
            publiceService.addAdminRecord(vo.getStoreId(), event,AdminRecordModel.Type.PASS_OR_REFUSE, vo.getAccessId());
            //消息通知u
            MessageLoggingModal messageLoggingSave = new MessageLoggingModal();
            messageLoggingSave.setStore_id(vo.getStoreId());
            messageLoggingSave.setType(MessageLoggingModal.Type.TYPE_GOODS_BRAND);
            messageLoggingSave.setParameter(supplierBrandModel.getBrand_id() + "");
            messageLoggingSave.setContent(text);
            messageLoggingSave.setAdd_date(new Date());
            messageLoggingSave.setSupplier_id(supplierBrandModel.getSupplier_id());
            messageLoggingModalMapper.insertSelective(messageLoggingSave);
        } catch (LaiKeAPIException l) {
            throw l;
        } catch (Exception e) {
            logger.error("审核供应商商品 异常", e);
            throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_WLYC, "网络异常", "examine");
        }
    }

}
